package hut.ai.team.object;

import hut.ai.team.screen.MainScreen;
import hut.ai.team.table.VanCo;

public class Chot {
	static QuanCo qTuong1;
	static QuanCo qTuong2;
	
	private static int[][][] score = { 
            { { 0,  0,  0,  0,  0,  0,  0,  0,  0},           /*Red*/
              { 0,  0,  0,  0,  0,  0,  0,  0,  0},
              { 0,  0,  0,  0,  0,  0,  0,  0,  0}, 
              {10,  0, 12,  0, 15,  0, 12,  0, 10},
              {10,  0, 13,  0, 10,  0, 13,  0, 10},
              {20, 20, 20, 20, 20, 20, 20, 20, 20},
              {20, 21, 21, 22, 22, 22, 21, 21, 20},
              {20, 21, 21, 23, 23, 23, 21, 21, 20},
              {20, 21, 21, 23, 22, 23, 21, 21, 20},
              {11, 12, 13, 14, 14, 14, 13, 12, 11}
            },
            { 
              {11, 12, 13, 14, 14, 14, 13, 12, 11},          /*Black*/
              {20, 21, 21, 23, 22, 23, 21, 21, 20},
              {20, 21, 21, 23, 23, 23, 21, 21, 20},
              {20, 21, 21, 22, 22, 22, 21, 21, 20},
              {20, 20, 20, 20, 20, 20, 20, 20, 20},
              {10,  0, 13,  0, 10,  0, 13,  0, 10},
              {10,  0, 12,  0, 15,  0, 12,  0, 10},
              { 0,  0,  0,  0,  0,  0,  0,  0,  0},
              { 0,  0,  0,  0,  0,  0,  0,  0,  0},
              { 0,  0,  0,  0,  0,  0,  0,  0,  0} }
          };
	
	public static int getDiemChot(QuanCo q){
		int d = 0;
		if(q.Phe == 2){
			if(q.TrangThai == 1)
				d = 20 + score[1][q.Hang][q.Cot];
			else if(q.TrangThai == 2)
				d = 10 + score[1][q.Hang][q.Cot];
			else d = 0;
		}else{
			if(q.TrangThai == 1)
				d = 20 + score[0][q.Hang][q.Cot];
			else if(q.TrangThai == 2)
				d = 10 + score[0][q.Hang][q.Cot];
			else
				d = 0;
		}
		return d;
	}
	
	public static void getQuanTuong() {
		if (MainScreen.getInstance().mode == 2) {
			for (int i = 0; i < MainScreen.getInstance().n1.dsQuanCo.size(); i++) {
				if (MainScreen.getInstance().n1.dsQuanCo.get(i).Loai == 1) {
					qTuong1 = MainScreen.getInstance().n1.dsQuanCo.get(i);
					break;
				}
			}
			for (int i = 0; i < MainScreen.getInstance().n2.dsQuanCo.size(); i++) {
				if (MainScreen.getInstance().n2.dsQuanCo.get(i).Loai == 1) {
					MainScreen.getInstance();
					qTuong2 = MainScreen.getInstance().n2.dsQuanCo.get(i);
					break;
				}
			}
		} else {
			for (int i = 0; i < MainScreen.getInstance().n1.dsQuanCo.size(); i++) {
				if (MainScreen.getInstance().n1.dsQuanCo.get(i).Loai == 1) {
					qTuong1 = MainScreen.getInstance().n1.dsQuanCo.get(i);
					break;
				}
			}
			for (int i = 0; i < MainScreen.getInstance().computer.dsQuanCo.size(); i++) {
				if (MainScreen.getInstance().computer.dsQuanCo.get(i).Loai == 1) {
					qTuong2 = MainScreen.getInstance().computer.dsQuanCo.get(i);
					break;
				}
			}
		}
	}

	public static int KiemTra(QuanCo c, int i, int j) {
		boolean turn = false;
		getQuanTuong();
		if (c.Phe == 1) {
			if (i >= 0 && i <= 4)
				if (i == c.Hang + 1 && j == c.Cot) {
					if (MainScreen.getInstance().mBanCo.ViTri[i][j].Trong == true)
						turn = true;
					if (MainScreen.getInstance().mBanCo.ViTri[i][j].Trong == false)
						if (MainScreen.getInstance().mBanCo.ViTri[i][j].Phe != c.Phe)
							turn = true;
				}
			if (i > 4 && i <= 9)
				if ((i == c.Hang + 1 && j == c.Cot)
						|| (i == c.Hang && j == c.Cot - 1)
						|| (i == c.Hang && j == c.Cot + 1))
					if (i >= 0 && i <= 9 && j >= 0 && j <= 8) {
						if (MainScreen.getInstance().mBanCo.ViTri[i][j].Trong == true)
							turn = true;
						if (MainScreen.getInstance().mBanCo.ViTri[i][j].Trong == false)
							if (MainScreen.getInstance().mBanCo.ViTri[i][j].Phe != c.Phe)
								turn = true;
					}
		}
		if (c.Phe == 2) {
			if ((i <= 9) && (i >= 5))
				if ((i == c.Hang - 1) && (j == c.Cot)) {
					if (MainScreen.getInstance().mBanCo.ViTri[i][j].Trong == true)
						turn = true;
					if (MainScreen.getInstance().mBanCo.ViTri[i][j].Trong == false)
						if (MainScreen.getInstance().mBanCo.ViTri[i][j].Phe != c.Phe)
							turn = true;
				}
			if ((i < 5) && (i >= 0))
				if ((i == c.Hang - 1 && j == c.Cot)
						|| (i == c.Hang && j == c.Cot - 1)
						|| (i == c.Hang && j == c.Cot + 1))
					if (i >= 0 && i <= 9 && j >= 0 && j <= 8) {
						if (MainScreen.getInstance().mBanCo.ViTri[i][j].Trong == true)
							turn = true;
						if (MainScreen.getInstance().mBanCo.ViTri[i][j].Trong == false)
							if (MainScreen.getInstance().mBanCo.ViTri[i][j].Phe != c.Phe)
								turn = true;
					}
		}
		// Kiểm tra chống tướng
		if (MainScreen.getInstance().mode == 2) {
			if (qTuong1.Cot == qTuong2.Cot && c.Cot == qTuong2.Cot) {
				int ct = 0;
				if (j != c.Cot) {
					MainScreen.getInstance().mBanCo.OCoTrong(c.Hang, c.Cot);
					for (int t = qTuong1.Hang + 1; t < qTuong2.Hang; t++) {
						if (MainScreen.getInstance().mBanCo.ViTri[t][c.Cot].Trong == false)
							ct++;
					}
					if (ct == 0)
						turn = false;
					MainScreen.getInstance().mBanCo.ViTri[c.Hang][c.Cot].Trong = false;
					MainScreen.getInstance().mBanCo.ViTri[c.Hang][c.Cot].Phe = c.Phe;
					MainScreen.getInstance().mBanCo.ViTri[c.Hang][c.Cot].TrangThai = c.TrangThai;
					if (MainScreen.getInstance().mBanCo.ViTri[c.Hang][c.Cot].TrangThai == 1)
						MainScreen.getInstance().mBanCo.ViTri[c.Hang][c.Cot].Loai = c.LoaiCL;
					else
						MainScreen.getInstance().mBanCo.ViTri[c.Hang][c.Cot].Loai = c.Loai;
				}
			}
		} else {
			if (qTuong1.Cot == qTuong2.Cot && c.Cot == qTuong1.Cot) {
				int ct = 0;
				if (j != c.Cot) {
					MainScreen.getInstance().mBanCo.OCoTrong(c.Hang, c.Cot);
					for (int t = qTuong2.Hang + 1; t < qTuong1.Hang; t++) {
						if (MainScreen.getInstance().mBanCo.ViTri[t][c.Cot].Trong == false)
							ct++;
					}
					if (ct == 0)
						turn = false;
					MainScreen.getInstance().mBanCo.ViTri[c.Hang][c.Cot].Trong = false;
					MainScreen.getInstance().mBanCo.ViTri[c.Hang][c.Cot].Phe = c.Phe;
					MainScreen.getInstance().mBanCo.ViTri[c.Hang][c.Cot].TrangThai = c.TrangThai;
					if (MainScreen.getInstance().mBanCo.ViTri[c.Hang][c.Cot].TrangThai == 1)
						MainScreen.getInstance().mBanCo.ViTri[c.Hang][c.Cot].Loai = c.LoaiCL;
					else
						MainScreen.getInstance().mBanCo.ViTri[c.Hang][c.Cot].Loai = c.Loai;
				}
			}
		}
		if (turn)
			return 1;
		else
			return 0;
	}

	public static int TuongAnToan(QuanCo c, int i, int j) {
		boolean turn = true;
		getQuanTuong();
		if (turn == true) {
			int ho, co, tempTrangThai, tempTTBiAn = 0;
			QuanCo temp = null;

			ho = c.Hang;
			co = c.Cot;
			tempTrangThai = c.TrangThai;
			if (MainScreen.getInstance().mode == 2) {
				if (MainScreen.getInstance().mBanCo.ViTri[i][j].Trong == false) {
					if (MainScreen.getInstance().mBanCo.ViTri[i][j].Phe == MainScreen.getInstance().n1.Phe) {
						for (int k = 0; k < MainScreen.getInstance().n1.dsQuanCo.size(); k++) {
							if (MainScreen.getInstance().n1.dsQuanCo.get(k).Hang == i
									&& MainScreen.getInstance().n1.dsQuanCo.get(k).Cot == j)
								temp = MainScreen.getInstance().n1.dsQuanCo.get(k);
						}
					} else {
						for (int k = 0; k < MainScreen.getInstance().n2.dsQuanCo.size(); k++) {
							if (MainScreen.getInstance().n2.dsQuanCo.get(k).Hang == i
									&& MainScreen.getInstance().n2.dsQuanCo.get(k).Cot == j)
								temp = MainScreen.getInstance().n2.dsQuanCo.get(k);
						}
					}
				}
			} else {
				if (MainScreen.getInstance().mBanCo.ViTri[i][j].Trong == false) {
					if (MainScreen.getInstance().mBanCo.ViTri[i][j].Phe == MainScreen.getInstance().computer.Phe) {
						for (int k = 0; k < MainScreen.getInstance().computer.dsQuanCo.size(); k++) {
							if (MainScreen.getInstance().computer.dsQuanCo.get(k).Hang == i
									&& MainScreen.getInstance().computer.dsQuanCo.get(k).Cot == j)
								temp = MainScreen.getInstance().computer.dsQuanCo.get(k);
						}
					} else {
						for (int k = 0; k < MainScreen.getInstance().n1.dsQuanCo.size(); k++) {
							if (MainScreen.getInstance().n1.dsQuanCo.get(k).Hang == i
									&& MainScreen.getInstance().n1.dsQuanCo.get(k).Cot == j)
								temp = MainScreen.getInstance().n1.dsQuanCo.get(k);
						}
					}
				}
			}
			// Giả sử quân cờ được đi để kiểm tra Tướng mình có bị chiếu ko
			MainScreen.getInstance().mBanCo.OCoTrong(c.Hang, c.Cot);
			MainScreen.getInstance().mBanCo.ViTri[i][j].Trong = false;
			MainScreen.getInstance().mBanCo.ViTri[i][j].Phe = c.Phe;
			MainScreen.getInstance().mBanCo.ViTri[i][j].TrangThai = 2;
			MainScreen.getInstance().mBanCo.ViTri[i][j].Loai = c.Loai;
			if (temp != null) {
				tempTTBiAn = temp.TrangThai;
				VanCo.AnQuanCo(temp);
			}

			c.Hang = i;
			c.Cot = j;
			c.TrangThai = 2;
			// Kiểm tra
			if (MainScreen.getInstance().mode == 2) {
				if (c.Phe == MainScreen.getInstance().n1.Phe) {
					if (VanCo.ChieuTuong(qTuong1) == true)
						turn = false;
				} else {
					if (VanCo.ChieuTuong(qTuong2) == true)
						turn = false;
				}
			} else {
				if (c.Phe == MainScreen.getInstance().n1.Phe) {
					if (VanCo.ChieuTuong(qTuong1) == true)
						turn = false;
				} else {
					if (VanCo.ChieuTuong(qTuong2) == true)
						turn = false;
				}
			}
			// Trả lại những gì đã giả sử ở trên ^^!
			c.Hang = ho;
			c.Cot = co;
			c.TrangThai = tempTrangThai;
			MainScreen.getInstance().mBanCo.OCoTrong(i, j);
			MainScreen.getInstance().mBanCo.ViTri[ho][co].Trong = false;
			MainScreen.getInstance().mBanCo.ViTri[ho][co].Phe = c.Phe;
			MainScreen.getInstance().mBanCo.ViTri[ho][co].TrangThai = c.TrangThai;
			if (MainScreen.getInstance().mBanCo.ViTri[ho][co].TrangThai == 1)
				MainScreen.getInstance().mBanCo.ViTri[ho][co].Loai = c.LoaiCL;
			else
				MainScreen.getInstance().mBanCo.ViTri[ho][co].Loai = c.Loai;
			if (temp != null) {
				if (temp.Phe != 0) {
					temp.TrangThai = tempTTBiAn;
					VanCo.HoiSinhQuanCo(temp);
					MainScreen.getInstance().mBanCo.ViTri[i][j].Trong = false;
					MainScreen.getInstance().mBanCo.ViTri[i][j].Phe = temp.Phe;
					MainScreen.getInstance().mBanCo.ViTri[i][j].TrangThai = temp.TrangThai;
					if (MainScreen.getInstance().mBanCo.ViTri[i][j].TrangThai == 1)
						MainScreen.getInstance().mBanCo.ViTri[i][j].Loai = temp.LoaiCL;
					else
						MainScreen.getInstance().mBanCo.ViTri[i][j].Loai = temp.Loai;
				}
			}
		}
		if (turn)
			return 1;
		else
			return 0;
	}
}
